<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>教练浏览|私教预约系统</title>
    <link href="../bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="../css/styles.css">
    <style type="text/css">

        p{
            font-family: tahoma,geneva,sans-serif;
            font-size: 24px;
            text-align: center;
        }
        table{
            margin-top: 30px;
        }
    </style>

</head>
<body>

<h3 class="col-sm-offset-5"><span class="label label-default">健身房教练介绍</span></h3>


<div class="col-sm-4 col-sm-offset-10">
    <select class = "selectpicker" id = "inputSortType" title="请选择" onclick="sort()">
        <option value="1">经验排序</option>
        <option value="2">年龄排序</option>
        <option value="3">男教练</option>
        <option value="4">女教练</option>
    </select>
</div>


<div id="container">
    <div class="col-xl-10">
        <table class="table table-bordered table-striped table-hover">
            <thead>
            <tr>
                <td>#</td>
                <td>教练名</td>
                <td>性别</td>
                <td>年龄</td>
                <td>执教经验（年）</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody id="tbody">

            </tbody>
        </table>
    </div>
</div>
</body>
<script src="../jquery/jquery-3.4.1.min.js"></script>
<script src="../bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function(){

        let sortIdExit = document.cookie.indexOf("trainersortid");

        if (sortIdExit === -1)
        {
            let sortId = 1;
            $.cookie('trainersortid', sortId);
        }

        let currentSortId = $.cookie("trainersortid");

        $("#inputSortType").val(currentSortId);

        if (currentSortId == 1)
        {
            jQuery.post("/getTrainersListOrderByExperience",function(rst){//处理响应的结果
                writeHTML(rst);
            },"json");//表示返回数据的格式
        }else if (currentSortId == 2){
            jQuery.post("/getTrainersListOrderByAge",function(rst){//处理响应的结果
                writeHTML(rst);
            },"json");//表示返回数据的格式
        }
        else if (currentSortId == 3){
            jQuery.post("/getTrainersListBySex", {"trainerSex": "男"}, function(rst){//处理响应的结果
                writeHTML(rst);
            },"json");//表示返回数据的格式
        }else {
            jQuery.post("/getTrainersListBySex", {"trainerSex": "女"}, function(rst){//处理响应的结果
                writeHTML(rst);
            },"json");//表示返回数据的格式
        }
    });

    function writeHTML(rst){
        let body="";
        for(let i = 0; i < rst.length; i++)
        {
            let json = rst[i];//{"#":1, "课程名": "教练"： ,"开课时间": , "课程类型": ，"操作"： }

            body += "<tr>";
            body += "<td>" + (i + 1) + "</td>";
            body += "<td>" + json.trainerName + "</td>";
            body += "<td>" + json.trainerSex + "</td>";
            body += "<td>" + json.trainerAge + "</td>";
            body += "<td>" + json.trainerExperience + "</td>";
            body += "<td>";
            body += "<a href='#' onclick='goTrainerDetail(\""+json.trainerPhone+"\")'>查看更多</a>";
            body += "</td>"
            body += "</tr>";
        }
        jQuery("#tbody").html(body);
    }

    $("#inputSortType").click(function (){

        setTimeout("temp()", 2000);

    })

    function temp(){
        let options = $("#inputSortType option:selected");
        let id = options.val();
        $.removeCookie("trainersortid", {path: '/'});
        $.cookie("trainersortid", id);
        window.location.reload();
    }


    function goTrainerDetail(phone){
        $.cookie('currentTrainerPhone', phone);
        window.location.replace("trainerDetails.html");
    }

</script>
</html>